package com.lyshop.guiyingshanding.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.lyshop.guiyingshanding.R;
import com.lyshop.guiyingshanding.bean.FollowOrderBean;
import com.lyshop.guiyingshanding.utils.GlideUtils;
import com.lyshop.guiyingshanding.utils.MedalUtils;
import com.lyshop.guiyingshanding.utils.Utils;
import com.lyshop.guiyingshanding.view.CustomTextView;
import com.lyshop.guiyingshanding.view.SignSeekBar;
import com.zc.mychart.utils.FormatUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class FollowOrderAdapter extends RecyclerView.Adapter {
    private List<FollowOrderBean> dataList = new ArrayList<>();
    private FollowOrderListener followOrderListener;

    public void setFollowOrderListener(FollowOrderListener followOrderListener) {
        this.followOrderListener = followOrderListener;
    }

    public void setDataList(List<FollowOrderBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_follow_order_info, parent, false);
        return new FollowOrderViewHolder(parent.getContext(), view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof FollowOrderViewHolder) {
            ((FollowOrderViewHolder) holder).setData(position);
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    private class FollowOrderViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private final ImageView iv_user_header;
        private final TextView tv_order_time;
        private final TextView tv_user_name;
        private final ImageView iv_follow_vip;
        private final TextView tv_win_rate;
        private final TextView tv_trade_type;
        private final TextView tv_product_name;
        private final SignSeekBar sb_seek;
        private final TextView btn_follow;
        private final TextView tv_k_line;
        private final TextView tv_describe;
        private final TextView tv_pro_name;
        private final CustomTextView ctv_profit;
        private Context mContext;
        private int mPosition;

        public FollowOrderViewHolder(Context context, @NonNull View itemView) {
            super(itemView);
            mContext = context;
            iv_user_header = itemView.findViewById(R.id.iv_user_header);
            tv_order_time = itemView.findViewById(R.id.tv_order_time);
            tv_user_name = itemView.findViewById(R.id.tv_user_name);
            iv_follow_vip = itemView.findViewById(R.id.iv_follow_vip);
            tv_win_rate = itemView.findViewById(R.id.tv_win_rate);
            tv_trade_type = itemView.findViewById(R.id.tv_trade_type);
            tv_product_name = itemView.findViewById(R.id.tv_product_name);
            sb_seek = itemView.findViewById(R.id.sb_seek);
            btn_follow = itemView.findViewById(R.id.btn_follow);
            tv_k_line = itemView.findViewById(R.id.tv_k_line);
            tv_describe = itemView.findViewById(R.id.tv_describe);
            tv_pro_name = itemView.findViewById(R.id.tv_pro_name);
            ctv_profit = itemView.findViewById(R.id.ctv_profit);

            iv_user_header.setOnClickListener(this);
            btn_follow.setOnClickListener(this);
            tv_k_line.setOnClickListener(this);
        }

        private void setData(int position) {
            mPosition = position;
            FollowOrderBean followOrderBean = dataList.get(position);
            final boolean tradeType = followOrderBean.trade_type == 2;

            GlideUtils.showImageOptions(mContext, followOrderBean.header_img, iv_user_header);
            tv_user_name.setText(followOrderBean.nickname);
            MedalUtils.setUserCenterLevelTag(followOrderBean.level, iv_follow_vip);
            tv_win_rate.setText(followOrderBean.win_lv + "%");
            tv_product_name.setText(followOrderBean.pro_name);
            if (tradeType) {
                int redColor = ContextCompat.getColor(mContext, R.color.buy_red);
                tv_trade_type.setText("现价订购");
                tv_trade_type.setTextColor(redColor);
                tv_trade_type.setBackgroundResource(R.drawable.shape_rect_round_2dp_ffe7e7);
            } else {
                int greenColor = ContextCompat.getColor(mContext, R.color.buy_green);
                tv_trade_type.setText("结算价订购");
                tv_trade_type.setTextColor(greenColor);
                tv_trade_type.setBackgroundResource(R.drawable.shape_rect_round_2dp_daf2e4);
            }
            tv_describe.setText("规格" + followOrderBean.unit_price + "元/件 ｜ 件数" + followOrderBean.amount + "件");
            if (followOrderBean.profitLoss > 0){
                ctv_profit.setText("+" + followOrderBean.profitLoss + "元");
                ctv_profit.setTextColor(Utils.getColor(mContext, true));
            }else {
                ctv_profit.setText(followOrderBean.profitLoss + "元");
                ctv_profit.setTextColor(Utils.getColor(mContext, false));
            }
            sb_seek.getConfigBuilder()
                    .setTopUnit("最新价 ").setUnit("订购价 ").reverse()
                    .format(new DecimalFormat("######.##"))
                    .min((float) followOrderBean.stop_loss).max((float) followOrderBean.target_profit)
                    .startProgress((float) followOrderBean.build_price)
                    .progress((float) followOrderBean.latest_price)
                    .multiple(followOrderBean.multiple)
                    .tradeType(tradeType)
                    .build();
            sb_seek.setEnabled(false);
            tv_order_time.setText(FormatUtil.getTimeStateNew(String.valueOf(followOrderBean.build_time)));
            tv_pro_name.setText(followOrderBean.product_name);
        }

        @Override
        public void onClick(View v) {
            if (followOrderListener == null) return;
            switch (v.getId()) {
                case R.id.iv_user_header:
                    followOrderListener.onClickDetail(dataList.get(mPosition).user_id);
                    break;
                case R.id.btn_follow:
                    followOrderListener.onClickFollow(mPosition);
                    break;
                case R.id.tv_k_line:
                    followOrderListener.onClickGoKLine(mPosition);
                    break;
            }
        }
    }

    public interface FollowOrderListener {
        void onClickDetail(String userId);

        void onClickFollow(int position);

        void onClickGoKLine(int position);
    }
}
